package io.gitee.kenewstar.dbmq.thread;

import io.gitee.kenewstar.dbmq.ConsumerTask;
import io.gitee.kenewstar.dbmq.data.DbDataHandler;

import java.util.concurrent.locks.LockSupport;

/**
 * @author kenewstar
 */
public class ConsumerThread extends CommonThread {

    private final ConsumerTask consumerTask;
    private final DbDataHandler dbDataHandler;

    public ConsumerThread(ConsumerTask consumerTask, DbDataHandler dbDataHandler) {
        this.dbDataHandler = dbDataHandler;
        this.consumerTask = consumerTask;
        this.setName("thread-" + consumerTask.getTopic());
    }

    @Override
    public void run() {
        threadStatus = ThreadStatus.RUNNING;
        while (true) {
            LOGGER.debug("==================topic====================={}", consumerTask.getTopic());
            try {
                if (threadStatus.equals(ThreadStatus.DEAD)) {
                    return;
                }
                consumerTask.executeTask(dbDataHandler);
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.threadStatus = ThreadStatus.WAITING;
            LockSupport.park();
        }

    }

}
